class UserCfgController < ApplicationController
  before_filter :set_cookies_or_url
  after_filter :log_filter,:except => [:index]
  layout "user_cfg"
  include UserCfgHelper
  def index
    redirect_to params.merge!(:action => 'user_cfg')
  end

  #我的地盘
  def user_cfg
    params[:cn] = params[:cn].to_i + 1
    @titles='我的地盘'
    @oper_type = 16
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    #取得用户信息
    @user_cfg = UserCfg.find_last_by_sid params[:sid]
    @resource = params[:resource]
    respond_to do |format|
      format.wml
    end
  end

  #我的收藏夹
  def user_collect
    params[:cn] = params[:cn].to_i + 1
    @titles='我的收藏'
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    if params[:jump_page].nil? && params[:total_pages].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i > params[:total_pages].to_i
        params[:page] = params[:total_pages]
      else
        params[:page] = params[:jump_page]
      end      
    end   
    @go_page = 1
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    to_page = params[:page]
    if not params[:page_for].nil?
      if not params[:page_for].empty?
        to_page = params[:page_for]
        params[:page_for] = nil
      end
    end
    @page_for = params[:page]
    if to_page.nil?
      to_page = 1
    end

    per_page = $res_per_page_word_collect
    if @img_style == 2.to_s
      per_page = $res_per_page_img
    end
    sid = params[:sid]
    @user_collects = UserCollect.paginate :per_page => per_page, :page =>  to_page,
      :conditions => ['sid =?', sid],
      :order => 'created_at desc'    
    @resource = params[:resource]
    respond_to do |format|
      format.wml
    end
  end

  #我的收藏资源
  def user_collect_res
    params[:cn] = params[:cn].to_i + 1
    @titles='我的收藏'
    @oper_type = 9
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end   
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end

    

    #当前图片
    @uc = UserCollect.find params[:uc_id]
    @uc_index = params[:uc_index]
    @uc_nexts = UserCollect.find :all,
      :conditions => ['user_id = ? and created_at < ?',@uc.user_id,@uc.created_at],
      :order => 'created_at desc',
      :limit =>'2'
    respond_to do |format|
      format.wml
    end
  end

  #删除我的收藏
  def user_collect_del
    params[:cn] = params[:cn].to_i + 1
    @oper_type = 11
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    #当前图片
    uc = UserCollect.find params[:uc_id]
    uc.destroy
    redirect_to params.merge!(:action => 'user_collect')
  end


  #成功收藏
  def user_collect_sucess
    params[:cn] = params[:cn].to_i + 1
    @titles='收藏成功'
    @oper_type = 8
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    @resource = params[:resource]
    index = params[:index]
    sid = params[:sid]

    #计算在图片数组里面的位置
    if @go_page.nil?
      @go_page = 1
    end
    index = @pp_num.to_i*@go_page.to_i - @pp_num.to_i + index.to_i    
   
    user_collect = UserCollect.find :all,
      :conditions => ['sid =? and res_id = ? and img_index=? ', sid,@resource,index]
    unless user_collect.size > 0
      user_collect = UserCollect.new
      user_cfg = UserCfg.find_last_by_sid params[:sid]
      user_collect.user_id = user_cfg.id
      user_collect.sid = sid
      user_collect.user_sign = user_cfg.user_sign
      user_collect.img_index = index
      res = Resource.find @resource
      user_collect.res_id = res.id
      user_collect.img_path = res.res_dir
      user_collect.res_name = res.name
      user_collect.save     
    end
    
    respond_to do |format|
      format.wml
    end
  end

  #收藏的套图显示
  def user_mapping
    params[:cn] = params[:cn].to_i + 1
    @titles='我的套图'
    @oper_type = 10
    @img_style = '1'
    if params[:img_style].nil?
      @img_style = '1'
    else
      @img_style = params[:img_style]
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    if not params[:jump_page].nil?
      if params[:jump_page].to_i == 0
        params[:jump_page] = 1
      end
      if params[:jump_page].to_i * $img_per_page > params[:total_pages].to_i
        params[:jump_page] = params[:total_pages].to_i * $img_per_page
      end
      if params[:jump_page].to_i % $img_per_page == 0
        params[:page] = (params[:jump_page].to_i / $img_per_page).to_i
      else
        params[:page] = (params[:jump_page].to_i / $img_per_page).to_i + 1
      end
      params[:id] = nil
    end    
    @page_for = params[:page_for]
    unless params[:page].nil?
      @go_page = params[:page]
    else
      @go_page = params[:go_page]
    end
    @uc = UserCollect.find params[:uc_id]
    type = $small
    if @pp_style.to_i == 2
      type = $larger
    end
    @img_arry = get_res_array_pagin(@uc.img_path,type,@pp_num)
    @resource = Resource.find @uc.res_id
    res_all = Resource.find :all,
      :conditions =>  ["col_id=?",@resource.col_id],
      :order => 'orders asc'
    res_all.each_with_index do |res,res_index|
      if res.id == @resource.id
        if res_index > 0
          @res_prev = res_all[res_index - 1]
        else
          @res_prev = res_all[res_index]
        end
        if res_index < res_all.size - 1
          @res_next = res_all[res_index + 1]
        else
          @res_next = nil
        end

        if res_index < res_all.size - 2
          @res_nnext = res_all[res_index + 2]
        else
          @res_nnext = nil
        end
      end
    end
    #套图标签
    @tag_arry = ResTag.find_all_by_res_id @resource.id
    respond_to do |format|
      format.wml
    end
  end

  #用户评论
  def user_comment
    params[:cn] = params[:cn].to_i + 1
    @titles='评论'
    @oper_type = 12
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    @resource = params[:resource]
    @ucs = UserComment.paginate :per_page => 10,:page => params[:page],
      :conditions => ['res_id = ?',@resource],
      :order => 'created_at desc'
    respond_to do |format|
      format.wml
    end
  end

  #用户注册
  def user_register
    params[:cn] = params[:cn].to_i + 1
    @titles='注册页面'
    @oper_type = 13
    respond_to do |format|
      format.wml
    end
  end

  def user_register_sucess
    params[:cn] = params[:cn].to_i + 1
    @titles='提交注册'
    @oper_type = 14
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]   
    if params[:qq].to_i.to_s.length == params[:qq].to_s.length && params[:qq].to_i > 10000
      #取得用户信息
      user_cfg = UserCfg.find_last_by_qq params[:qq]
      #相同的qq没有被注册过
      if user_cfg.nil?
        user_cfg = UserCfg.find_last_by_sid params[:sid]
        user_cfg.qq = params[:qq]
        if params[:pw].nil? || params[:pw].to_s.length < 4
          user_cfg.password = params[:qq].to_s[params[:qq].to_s.length-4,params[:qq].to_s.length]
        else
          user_cfg.password = params[:pw]
        end
        if !params[:phone].nil? && params[:phone].to_i.to_s.length == params[:phone].to_s.length && params[:phone].to_s.length > 10 && params[:phone].to_s.length < 14
          user_cfg.phone = params[:phone]
        end
        user_cfg.update_attributes({:password=>user_cfg.password,:qq=>user_cfg.qq,:phone=>user_cfg.phone,:sex=>params[:sex],:user_type=>2,:nickname=>user_cfg.qq})
        redirect_to params.merge!(:action => 'user_register_suc')
      else
        redirect_to params.merge!(:action => 'user_register_failed',:type => '1')
      end
    else
      redirect_to params.merge!(:action => 'user_register_failed',:type => '2')
    end
  end

  def user_register_suc
    params[:cn] = params[:cn].to_i + 1
    @titles='注册成功'
    @oper_type = 16
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    respond_to do |format|
      format.wml
    end
  end

  #注册错误提示
  def user_register_failed
    params[:cn] = params[:cn].to_i + 1
    @titles='注册失败'
    @oper_type = 15
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    type = params[:type]
    @mes = ""
    if type == '1'
      @mes = "您输入的QQ号码已经被注册了<br/>"
    elsif type =='2'
      @mes = "您输入的qq号错误.<br/>请输入正确的qq号码<br/>"
    else
      @mes = "系统错误，请稍后再试试<br/>"
    end
    respond_to do |format|
      format.wml
    end
  end

  #修改昵称
  def user_cfg_modify_nickname
    params[:cn] = params[:cn].to_i + 1
    @titles='修改昵称'
    @oper_type = 17
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    @mes = ""
    @uc = UserCfg.find_last_by_sid params[:sid]
    #修改昵称
    if !params[:nickname].nil? && params[:nickname].to_s.length > 0 && params[:type].nil?      
      @uc.update_attributes({:nickname=> params[:nickname]})
      redirect_to params.merge!(:action => 'user_cfg')
    else
      @mes = "请输入昵称"
      respond_to do |format|
        format.wml
      end
    end
  end

  #修改手机号
  def user_cfg_modify_phone
    params[:cn] = params[:cn].to_i + 1
    @titles='修改手机号'
    @oper_type = 18
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    @mes = ""
    @uc = UserCfg.find_last_by_sid params[:sid]
    #修改号码
    if !params[:phone].nil? && params[:phone].to_i.to_s.length == params[:phone].to_s.length && params[:phone].to_s.length > 10 && params[:phone].to_s.length < 14 && params[:type].nil?  
      @uc.update_attributes({:phone=> params[:phone]})
      redirect_to params.merge!(:action => 'user_cfg')
    else
      @mes = "请输入手机号"
      respond_to do |format|
        format.wml
      end
    end
  end

  #修改密码
  def user_cfg_modify_pw
    params[:cn] = params[:cn].to_i + 1
    @titles='修改密码'
    @oper_type = 19
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    @mes = ""
    @uc = UserCfg.find_last_by_sid params[:sid]
    #修改号码
    if !params[:pw].nil? && params[:pw].to_s.length >= 4 && params[:type].nil?
      @uc.update_attributes({:password=> params[:pw]})
      redirect_to params.merge!(:action => 'user_cfg')
    else
      @mes = "请输密码"
      respond_to do |format|
        format.wml
      end
    end
  end

  #修改Q
  def user_cfg_modify_qq
    params[:cn] = params[:cn].to_i + 1
    params[:cn] = params[:cn].to_i + 1
    @titles='修改QQ'
    @oper_type = 20
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    @mes = ""
    @uc = UserCfg.find_last_by_sid params[:sid]
    #修改号码
    if !params[:qq].nil? && params[:qq].to_s.length >= 5 && params[:type].nil?
      @uc.update_attributes({:qq=> params[:qq]})
      redirect_to params.merge!(:action => 'user_cfg')
    else
      @mes = "请输QQ号码"
      respond_to do |format|
        format.wml
      end
    end
  end

  #修改性别
  def user_cfg_modify_sex
    params[:cn] = params[:cn].to_i + 1
    @titles='修改性别'
    @oper_type = 21
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]    
    @uc = UserCfg.find_last_by_sid params[:sid]
    #修改性别
    if !params[:sex].nil? && params[:type].nil?      
      @uc.update_attributes({:sex=> params[:sex].to_i})
      redirect_to params.merge!(:action => 'user_cfg')
    else     
      respond_to do |format|
        format.wml
      end
    end
  end

  #个性设置浏览页
  def user_cfg_setup
    params[:cn] = params[:cn].to_i + 1
    @titles='个性设置'
    @oper_type = 21
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]    
    @user_cfg = UserCfg.find_last_by_sid params[:sid]
    @mes = nil
    unless params[:type].nil?
      if params[:type].to_i == 1
        @user_cfg.update_attributes({:pp_style=> 1})
        @pp_style = 1
      elsif params[:type].to_i == 2
        @user_cfg.update_attributes({:pp_style=> 2})
        @pp_style = 2
      elsif params[:type].to_i == 3
        @user_cfg.update_attributes({:pp_num=> params[:p_num]})
        @pp_num = params[:p_num]
      elsif params[:type].to_i == 4
        @user_cfg.update_attributes({:img_style=> 2})
        @img_style = 2
      elsif params[:type].to_i == 5
        @user_cfg.update_attributes({:img_style=> 1})
        @img_style = 1
      end
      @mes = "设置成功.请注意保存!"
    end
    @resource = params[:resource]
    respond_to do |format|
      format.wml
    end
  end

  #登录
  def user_cfg_login
    params[:cn] = params[:cn].to_i + 1
    @titles='用户登录'
    @oper_type = 21
    @img_style = params[:img_style]
    if @img_style.nil?
      @img_style = 1
    end
    @pp_style = params[:pp_style]
    if @pp_style.nil?
      @pp_style = 1
    end
    @pp_num = params[:pp_num]
    if @pp_num.nil?
      @pp_num = 2
    end
    @go_page = params[:go_page]
    @page_for = params[:page_for]
    @mes = nil
    @smes = nil
    unless params[:ltype].nil?
      if !params[:qq].nil? && params[:qq].to_i.to_s.length >= 5
        if params[:pw].nil? || params[:pw].to_i == 0
          pw = params[:qq].to_s[params[:qq].to_s.length-4,params[:qq].to_s.length]         
        else
          pw = params[:pw]
        end
        user_cfg = UserCfg.find_last_by_qq_and_password params[:qq],pw
        unless user_cfg.nil?
          @img_style = user_cfg.img_style
          @pp_style = user_cfg.pp_style
          @pp_num = user_cfg.pp_num
          params[:sid] = user_cfg.sid
          cookies['sid'] = { :value => params[:sid], :expires => 1.year.from_now }
          @smes = "成功登录"
        else
          @mes = "您输入的QQ或密码有误，请重新输入"
        end
      else
        @mes = "请输入QQ号码"
      end
    end
    respond_to do |format|
      format.wml
    end
  end
end
